#####
#####
###     R session that creates Figure 2
#####
#####

library(ellipse)
library(lattice)


###
###   Read original MDS configuration from file,
###   "cmds config, with names.txt"
###

config <- read.table(file.choose(), header = T)


###
###   Read bootstrap replication configurations
###   from file, "bootstrap estims, with names.txt"
###

bs.reps <- read.table(file.choose(), header = T)


#####
#####
###     Reflect the horizontal axis, so liberal candidates
###     are on the left, etc.
#####
#####

config$dim1 <- -config$dim1

bs.reps$dim1 <- -bs.reps$dim1


#####
#####
###     Use ellipse function to calculate data
###     ellipse coordinates for each candidate
###     and party
#####
#####

kerry <- bs.reps[bs.reps$name == "kerry",]

ekerry <- ellipse(cov(kerry[,2:3]), centre = c(config[2,2], config[2,3]))

ekerry <- as.data.frame(ekerry)

mccain <- bs.reps[bs.reps$name == "mccain",]

emccain <- ellipse(cov(mccain[,2:3]), centre = c(config[11,2], config[11,3]))

emccain <- as.data.frame(emccain)

gwbush <- bs.reps[bs.reps$name == "gwbush",]

egwbush <- as.data.frame(ellipse(cov(gwbush[,2:3]), centre = c(config[1,2],
config[1,3])))

nader <- bs.reps[bs.reps$name == "nader",]

enader <- as.data.frame(ellipse(cov(nader[,2:3]), centre = c(config[3,2],
config[3,3])))

cheney <- bs.reps[bs.reps$name == "cheney",]

echeney <- as.data.frame(ellipse(cov(cheney[,2:3]), centre = c(config[4,2],
config[4,3])))

edwards <- bs.reps[bs.reps$name == "edwards",]

eedwards <- as.data.frame(ellipse(cov(edwards[,2:3]), centre = c(config[5,2],
config[5,3])))

lbush <- bs.reps[bs.reps$name == "lbush",]

elbush <- as.data.frame(ellipse(cov(lbush[,2:3]), centre = c(config[6,2],
config[6,3])))

hclinton <- bs.reps[bs.reps$name == "hclinton",]

ehclinton <- as.data.frame(ellipse(cov(hclinton[,2:3]), centre = c(config[7,2],
config[7,3])))

bclinton <- bs.reps[bs.reps$name == "bclinton",]

ebclinton <- as.data.frame(ellipse(cov(bclinton[,2:3]), centre = c(config[8,2],
config[8,3])))

powell <- bs.reps[bs.reps$name == "powell",]

epowell <- as.data.frame(ellipse(cov(powell[,2:3]), centre = c(config[9,2],
config[9,3])))

ashcroft <- bs.reps[bs.reps$name == "ashcroft",]

eashcroft <- as.data.frame(ellipse(cov(ashcroft[,2:3]), centre = c(config[10,2],
config[10,3])))

dempty <- bs.reps[bs.reps$name == "dempty",]

edempty <- as.data.frame(ellipse(cov(dempty[,2:3]), centre = c(config[12,2],
config[12,3])))

reppty <- bs.reps[bs.reps$name == "reppty",]

ereppty <- as.data.frame(ellipse(cov(reppty[,2:3]), centre = c(config[13,2],
config[13,3])))


#####
#####
###     Plot bootstrap confidence ellipses 
###
###     This function creates Figure 2.
#####
#####

xyplot(dim2 ~ dim1, data = config,
   aspect = 1,
   panel = function( x, y) {
   panel.xyplot(ekerry$dim1, ekerry$dim2, type = "l", 
     col = "black")
   panel.text(-1.87, 0.16, label = "Kerry", cex = .65, adj = .4)
   panel.xyplot(emccain$dim1, emccain$dim2, type = "l", 
     col = "black")
   panel.text(-0.12, 0.5, label = "McCain", cex = .65, adj = .9)
   panel.xyplot(egwbush$dim1, egwbush$dim2, type = "l", 
     col = "black")
   panel.text(1.2, 0.32, label = "G. Bush", cex = .65)
   panel.xyplot(enader$dim1, enader$dim2, type = "l", 
     col = "black")
   panel.text(-0.84, 1.994, label = "Nader", cex = .65, adj = .7)
   panel.xyplot(echeney$dim1, echeney$dim2, type = "l", 
     col = "black")
   panel.text(1.62, -.64, label = "Cheney", cex = .65, adj = 0)
   panel.xyplot(eedwards$dim1, eedwards$dim2, type = "l", 
     col = "black")
   panel.text(-1.58, .18, label = "Edwards", cex = .65, adj = 0)
   panel.xyplot(elbush$dim1, elbush$dim2, type = "l", 
     col = "black")
   panel.text(.71, -0.77, label = "L. Bush", cex = .65)
   panel.xyplot(ehclinton$dim1, ehclinton$dim2, type = "l", 
     col = "black")
   panel.text(-1.23, -1.25, label = "H. Clinton", cex = .65)
   panel.xyplot(ebclinton$dim1, ebclinton$dim2, type = "l", 
     col = "black")
   panel.text(-.73, .12, label = "B. Clinton", cex = .65, adj = 0)
   panel.xyplot(epowell$dim1, epowell$dim2, type = "l", 
     col = "black")
   panel.text(-.03, -.12, label = "Powell", cex = .65, adj = .9)
   panel.xyplot(eashcroft$dim1, eashcroft$dim2, type = "l", 
     col = "black")
   panel.text(1.7, .45, label = "Ashcroft", cex = .65)
   panel.xyplot(edempty$dim1, edempty$dim2, type = "l", 
     col = "blue")
   panel.text(-1.49, -.58, label = "Dem. Pty.", cex = .65, adj = .9)
   panel.xyplot(ereppty$dim1, ereppty$dim2, type = "l", 
     col = "red")
   panel.text(1.4, -.57, label = "Rep. Pty.", cex = .65, adj = 1)
   },
   ylim = c(-1.5, 2.5),
   xlim = c(-2, 2),
   xlab = "MDS Axis 1",
   ylab = "MDS Axis 2"
)



